package com.lwt.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lwt.gulimall.product.entity.AttrAttrgroupRelation;
import com.lwt.gulimall.product.entity.AttrGroup;
import com.lwt.gulimall.product.service.AttrAttrgroupRelationService;
import com.lwt.gulimall.product.service.CategoryService;
import com.lwt.gulimall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lwt.gulimall.product.entity.Attr;
import com.lwt.gulimall.product.service.AttrService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwt.gulimall.common.utils.R;



/**
 * 商品属性
 *
 * @author liuweitao
 * @email liuweitao2541@gmail.com
 * @date 2024-01-26 16:11:43
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/base/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") String catId){
        long pageSize = Long.parseLong((String)params.get("page"));
        long limit = Long.parseLong((String)params.get("limit"));
        String key = (String)params.get("key");
        Page<Attr> page = new Page<>(pageSize,limit);

        LambdaQueryWrapper<Attr> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(catId)&&!catId.equals("0")){
            wrapper.eq(Attr::getCatelogId, catId);
        }
        if(StringUtils.isNotBlank(key)){
            wrapper.and(w->w.eq(Attr::getAttrId,key)
                    .or()
                    .like(Attr::getAttrName,key));
        }
        Page<Attr> pageResult = attrService.page(page, wrapper);
/*        for (Attr record : pageResult.getRecords()) {
            record
        }*/

        return R.ok().put("page", pageResult);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		Attr attr = attrService.getById(attrId);
        AttrVo attrVo = new AttrVo();
        BeanUtils.copyProperties(attr,attrVo);
        Long[] categoryPath = categoryService.getCategoryPath(attr.getCatelogId());
        attrVo.setCatelogIds(categoryPath);
        return R.ok().put("attr", attrVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrVo attrVo){

        Attr attr = new Attr();
        BeanUtils.copyProperties(attrVo,attr);
        attrService.save(attr);
        AttrAttrgroupRelation attrAttrgroupRelation = new AttrAttrgroupRelation();
        attrAttrgroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
        attrAttrgroupRelation.setAttrId(attr.getAttrId());
        attrAttrgroupRelationService.save(attrAttrgroupRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
        Attr attr = new Attr();
        BeanUtils.copyProperties(attrVo,attr);
		attrService.updateById(attr);
        AttrAttrgroupRelation attrAttrgroupRelation = new AttrAttrgroupRelation();
        BeanUtils.copyProperties(attrVo,attrAttrgroupRelation);

		attrAttrgroupRelationService.update(new LambdaUpdateWrapper<AttrAttrgroupRelation>().eq(AttrAttrgroupRelation::getAttrId,attrAttrgroupRelation.getAttrId())
                .set(AttrAttrgroupRelation::getAttrGroupId,attrAttrgroupRelation.getAttrGroupId()));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
